package com.guigu.controller;

import com.guigu.exception.system.LoginException;
import com.guigu.pojo.stock.SysUsers;
import com.guigu.service.stock.SysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/erp")
public class ErpController {

    @Autowired
    private SysUsersService sysUsersService;

    @GetMapping("/Login")
    public String Login() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public Boolean login(@RequestBody SysUsers user, HttpServletRequest request) throws LoginException {
        SysUsers sysUsers = sysUsersService.login(user);
        if (sysUsers != null) {
            // 判断用户状态是否为禁用
            if(sysUsers.getStatus() == 1) {
                throw new LoginException("该用户已被禁用，请联系管理员！！！");
            }
            // 根据用户id查询用户信息及角色信息
            SysUsers users = sysUsersService.selectSysUserById(sysUsers.getId());
            // 登录成功，将用户信息存入session中
            request.getSession().setAttribute("user", users);
            return true;
        }else {
            // 登录失败，抛出异常
            throw new LoginException("账号或密码登录错误，请重试！！！");
        }
    }

    @GetMapping("/home")
    public String home() {
        return "jsp/home";
    }

    @GetMapping("/Welcome")
    public String Welcome() {
        return "jsp/welcome";
    }

    @GetMapping("/foot")
    public String foot(){
        return "jsp/design/common/queryfoot";
    }
}
